<template>
  <div>
    <h3 style="padding: 0 10px">
      尊敬的{{ username }}伙伴，{{ $route.query.name }}财务数据如下
    </h3>
    <van-collapse v-model="activeName">
      <!--     利润表 -->
      <van-collapse-item title="利润表" name="1">
        <div class="collapse_item">
          <InputDate :timeValue="inconmeTimeValue" type="year-month" @confirm="inconmeChange"
                     style="margin-bottom: 10px"/>
          <IncomeTable :data="inconmeData"/>
        </div>
      </van-collapse-item>
      <!--      损益明细表-->
      <van-collapse-item title="损益明细表" name="2">
        <div class="collapse_item">
          <InputDate :timeValue="loseTimeValue" type="year-month" @confirm="loseChange"
                     style="margin-bottom: 10px"/>
          <LoseTable :data="loseData"/>
        </div>
      </van-collapse-item>
      <!--      资产负债表-->
      <van-collapse-item title="资产负债表" name="3">
        <div class="collapse_item">
          <InputDate :timeValue="assetsTimeValue" type="year-month" @confirm="assetsChange"
                     style="margin-bottom: 10px"/>
          <AssetsTable :data="assetsData"/>
        </div>
      </van-collapse-item>
      <!--      经营分析表-->
      <van-collapse-item title="经营分析表" name="4">
        <div class="collapse_item">
          <InputDate :timeValue="manageTimeValue" type="year-month" @confirm="manageChange"
                     style="margin-bottom: 10px"/>
          <ManageTable :data="manageData"/>
        </div>
      </van-collapse-item>
      <!--      季度预算表-->
      <van-collapse-item title="季度预算表" name="5">
        <div class="collapse_item">
          <selectQuarter :data="quarterSelect" style="margin-bottom: 10px" @confirm="quarterConfirm"/>
          <QuarterBudgetTable :data="quarterData"/>
        </div>
      </van-collapse-item>
    </van-collapse>
    <div class="agreement" @click="$router.push('/agreement')">
      <van-icon name="question"/>
      <span style="margin-left: 3px">名词解析</span>
    </div>
  </div>
</template>

<script>
import {dateFormat, setLoading, clearToast} from '@/utils'
import InputDate from '@/components/InputDate'
import selectQuarter from '@/components/selectQuarter'
// 利润表
import IncomeTable from "./IncomeTable";
// 损益明细表
import LoseTable from "./loseTable";
// 经营分析表
import ManageTable from "./manageTable";
// 资产负债表
import AssetsTable from "./assetsTable";
// 季度预算表
import QuarterBudgetTable from './quarterBudgetTable'
import {
  fetchFinanceProfit,
  fetchFinanceBusinessAnalysis,
  fetchFinanceLose,
  fetchFinanceAssets,
  fetchFinanceMonth,
  fetchFinanceQuarter
} from '@/api/index'

export default {
  name: "index",
  components: {
    InputDate,
    IncomeTable,
    AssetsTable,
    ManageTable,
    LoseTable,
    QuarterBudgetTable,
    selectQuarter
  },
  data() {
    // 获取上一月时间
    return {
      activeName: ['1'],
      inconmeTimeValue: null,
      loseTimeValue: null,
      manageTimeValue: null,
      assetsTimeValue: null,
      quarterSelect: {},
      routerQuery: this.$route.query,
      inconmeData: {},
      loseData: {},
      manageData: {},
      assetsData: {},
      quarterData: {}
    }
  },
  computed: {
    username() {
      return sessionStorage.getItem('name') || ''
    }
  },
  created() {
    this.getMonth()
  },
  methods: {
    async inconmeChange(value) {
      setLoading()
      this.inconmeTimeValue = value
      await this.getFinanceProfit()
      clearToast()
    },
    async loseChange(value) {
      setLoading()
      this.loseTimeValue = value
      await this.getFinanceLose()
      clearToast()
    },
    async manageChange(value) {
      setLoading()
      this.manageTimeValue = value
      await this.getFinanceBusinessAnalysis()
      clearToast()
    },
    async assetsChange(value) {
      setLoading()
      this.assetsTimeValue = value
      await this.getFinanceAssets()
      clearToast()
    },
    async quarterConfirm(value) {
      setLoading()
      this.quarterSelect.one = value
      await this.getFinanceQuarter()
      clearToast()
    },
    // 获取最新有数据的月份
    async getMonth() {
      setLoading()
      const res = await fetchFinanceMonth({businessId: this.routerQuery.id})
      if (res.code === 200) {
        const {
          finance_assets_liabilities,
          finance_business_analysis,
          finance_operating_budget,
          finance_profit,
          finance_profit_and_loss_details
        } = res.data
        this.inconmeTimeValue = finance_profit.month
        this.manageTimeValue = finance_business_analysis.month
        this.loseTimeValue = finance_profit_and_loss_details.month
        this.assetsTimeValue = finance_assets_liabilities.month
        this.quarterSelect = finance_operating_budget
        const p1 = this.getFinanceProfit()
        const p2 = this.getFinanceBusinessAnalysis()
        const p3 = this.getFinanceLose()
        const p4 = this.getFinanceAssets()
        const p5 = this.getFinanceQuarter()
        Promise.all([p1, p2, p3, p4, p5]).then(res => {
          clearToast()
        }).catch(error => {
          clearToast()
        })
      }
    },
    // 获取利润表
    async getFinanceProfit() {
      const res = await fetchFinanceProfit({businessId: this.routerQuery.id, month: this.inconmeTimeValue})
      if (res.code === 200) {
        this.inconmeData = res.data
      } else if (res.code === 201) {

        this.inconmeData = {}
      }
    },

    //获取经营分析表
    async getFinanceBusinessAnalysis() {
      const res = await fetchFinanceBusinessAnalysis({businessId: this.routerQuery.id, month: this.manageTimeValue})
      if (res.code === 200) {
        this.manageData = res.data
      } else if (res.code === 201) {
        this.manageData = {}
      }
    },
    //获取损益明细表
    async getFinanceLose() {
      const res = await fetchFinanceLose({businessId: this.routerQuery.id, month: this.loseTimeValue})
      if (res.code === 200) {
        this.loseData = res.data
      } else if (res.code === 201) {
        this.loseData = {
          money: {},
          this_new: {},
          this_year: {}
        }
      }
    },
    //获取资产负债
    async getFinanceAssets() {
      const res = await fetchFinanceAssets({businessId: this.routerQuery.id, month: this.assetsTimeValue})
      if (res.code === 200) {
        const {this_month, this_year_start} = res.data
        this.assetsData = {
          this_month: this_month || {},
          this_year_start: this_year_start || {}
        }
      } else if (res.code === 201) {
        this.assetsData = {}
      }
    },
    // 获取季度预算表
    async getFinanceQuarter() {
      const select = this.quarterSelect.one
      if (select) {
        const res = await fetchFinanceQuarter({businessId: this.routerQuery.id, quarterId: select.id})
        if (res.code === 200) {
          this.quarterData = JSON.parse(res.data.quarter_list.content)
        } else if (res.code === 201) {
          this.quarterData = {}
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
.collapse_item {
  margin-bottom: 15px;

  .title {
    font-size: 16px;
    padding: 10px 0;
  }


}

.agreement {
  margin-top: 10px;
  padding: 0 15px;
  color: #929191;
  font-size: 13px;
  display: inline-block;
}
</style>
